<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <template slot="footer" v-if="disableSubmit">
      <a-button @click="handleCancel">关闭</a-button>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则名称">
          <a-input placeholder="请输入规则名称" v-decorator="['ruleName', validatorRules.ruleName]" :readOnly="disableSubmit"/>
        </a-form-item>
        <a-form-item
          v-show="showRuleColumn"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则字段">
          <a-input placeholder="请输入规则字段" v-decorator="['ruleColumn', validatorRules.ruleColumn]" :readOnly="disableSubmit"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="条件规则">
          <j-dict-select-tag
            @change="handleChangeRuleCondition"
            v-decorator="['ruleConditions', validatorRules.ruleConditions]"
            placeholder="请输入条件规则"
            :triggerChange="true"
            dictCode="rule_conditions"
            :readOnly="disableSubmit"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则值">
          <a-input placeholder="请输入规则值" v-decorator="['ruleValue', validatorRules.ruleValue]" :readOnly="disableSubmit"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import { addDataRule, editDataRule } from '../service'
  import { addPermissionRule, editPermissionRule } from '../../permission/service'
  import pick from 'lodash.pick'

  export default {
    name: 'SysDataRuleModal',
    data() {
      return {
        queryParam: {},
        title: '操作',
        visible: false,
        model: {},
        disableSubmit: false,
        ruleConditionList: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        // permissionId: '',
        validatorRules: {
          ruleConditions: { rules: [{ required: true, message: '请选择条件!' }] },
          ruleName: { rules: [{ required: true, message: '请输入规则名称!' }] },
          ruleValue: { rules: [{ required: true, message: '请输入规则值!' }] },
          ruleColumn: { rules: [] }
        },
        url: {
        },
        showRuleColumn: true,
        delFlag: 0
      }
    },
    created() {
    },
    methods: {
      add(permId) {
        let params = { permissionId: permId };
        this.edit(params)
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        if (record.delFlag) {
            this.delFlag = this.model.delFlag;
        }
        this.initRuleCondition()
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'ruleName', 'ruleColumn', 'ruleConditions', 'ruleValue'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            console.log('permissionId = ' + this.model.permissionId);
            let formData = Object.assign(this.model, values)
            if (formData.ruleColumn && formData.ruleColumn.length > 0) {
              formData.ruleColumn = formData.ruleColumn.trim()
            }
            if (formData.ruleValue && formData.ruleValue.length > 0) {
              formData.ruleValue = formData.ruleValue.trim()
            }
            let obj;
            if (this.model.permissionId) {
              if (!this.model.id) {
                obj = addPermissionRule(formData)
              } else {
                obj = editPermissionRule(formData)
              }
            } else {
              if (!this.model.id) {
                obj = addDataRule(formData)
              } else {
                obj = editDataRule(formData)
              }
            }
            obj.then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                if (formData.permissionId) {
                  this.$emit('permDataRuleFinish');
                } else {
                  that.$emit('ok')
                }
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
      initRuleCondition() {
        if (this.model.ruleConditions && this.model.ruleConditions === 'USE_SQL_RULES') {
          this.showRuleColumn = false
        } else {
          this.showRuleColumn = true
        }
      },
      handleChangeRuleCondition(val) {
        if (val === 'USE_SQL_RULES') {
          this.form.setFieldsValue({
            ruleColumn: ''
          })
          this.showRuleColumn = false
        } else {
          this.showRuleColumn = true
        }
      }
    }
  }
</script>

<style scoped>

</style>
